<template>
  <a-modal
    v-model="visible"
    title="安排面试"
    @ok="handleOk"
    :confirm-loading="confirmLoading"
    okText="下一步"
    @cancel="$emit('update:visible', false)"
  >
    <a-form :form="form" :label-col="{ span: 4 }" :wrapper-col="{ span: 18 }">
      <a-form-item label="面试方式">
        <a-radio-group v-decorator="['interviewType', { initialValue: '1' }, { rules: [{ required: true }] }]">
          <a-radio value="1"> 现场面试 </a-radio>
          <a-radio value="2"> 电话面试 </a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="面试时间" style="margin-bottom: 0">
        <a-input v-show="false" placeholder="请输入" v-decorator="['sss', { rules: [{ required: true }] }]" />
        <a-form-item style="float: left; width: calc(50% - 12px)">
          <a-date-picker
            placeholder="请输入"
            format="YYYY-MM-DD"
            v-decorator="['date', { rules: [{ required: true, message: '日期不能为空' }] }]"
          />
        </a-form-item>

        <span :style="{ float: 'left', display: 'inline-block', width: '24px', textAlign: 'center' }"> - </span>
        <a-form-item style="float: left; width: calc(50% - 12px)">
          <a-time-picker
            style="width: 100%"
            format="HH:mm"
            v-decorator="['time', { rules: [{ required: true, message: '时间不能为空' }] }]"
            placeholder="请输入"
          />
        </a-form-item>
      </a-form-item>
      <a-form-item label="面试人">
        <a-input placeholder="请输入" v-decorator="['interviewPersonName', { rules: [{ required: true, message: '面试人不能为空' }] }]" />
      </a-form-item>
      <a-form-item label="电话">
        <a-input
          placeholder="请输入"
          v-decorator="['phone', { rules: [{ required: true, message: '' }, { validator: validatorPhone }] }]"
        />
      </a-form-item>
      <a-form-item label="面试地点">
        <a-input placeholder="请输入" v-decorator="['interviewLocation', { rules: [{ required: true, message: '面试地点不能为空' }] }]" />
      </a-form-item>
      <a-form-item label="备注">
        <a-textarea :maxLength="100" v-decorator="['remarks']" placeholder="请输入" :auto-size="{ minRows: 8, maxRows: 10 }" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import moment from "moment";
import { postArrangeAnInterview, postDeliverRecords, postAddDeliverRecord, getInfoById } from "../api";
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    deliverRecordId: {
      type: String,
      default: ""
    },
    talentPoolId: {
      type: String,
      default: ""
    },
    mjson: {
      type: Object,
      default: () => {}
    },
    info: {
      type: Object,
      default: () => {}
    },
    positionValue: {
      type: Object,
      default: () => {}
    },
    positionType: {
      type: String,
      default: ""
    }
  },
  watch: {
    visible(v) {
      if (v) {
        this.form.resetFields();
        setTimeout(() => {
          this.form.setFieldsValue({
            sss: "123"
          });
        }, 100);
      }
    }
  },
  data() {
    const validatorPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error("手机号不能为空"));
      } else {
        if (!/^1[3456789]\d{9}$/.test(value)) {
          callback(new Error("手机号格式不正确"));
        } else {
          callback();
        }
      }
    };
    return {
      validatorPhone,
      confirmLoading: false,
      form: this.$form.createForm(this, {})
    };
  },
  methods: {
    moment,
    async onPostArrangeAnInterview(v, mjson) {
      try {
        const { data } = await postArrangeAnInterview({ ...mjson, ...v });
        this.confirmLoading = false;
        this.$notification.success({ message: "操作成功" });
        this.$emit("update:visible", false);
        if (this.positionValue) this.$emit("onShareDeliverRecordId", v.deliverRecordId);
        this.$emit("onShare", data);
      } catch (err) {
        this.confirmLoading = false;
        console.log("error log --> ", err);
      }
    },
    handleOk() {
      this.form.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true;
          values.deliverRecordId = this.deliverRecordId;
          values.talentPoolId = this.talentPoolId;
          // values.interviewPersonName = this.info.name;
          // values.phone = this.info.phone;
          values.interviewDate = moment(values.date).format("YYYY-MM-DD") + " " + moment(values.time).format("HH:mm") + ":00";
          delete values.date;
          delete values.time;
          delete values.sss;
          // values.operationJobSeekerRecordId
          if (this.positionValue) {
            this.onPostDeliverRecords(values);
          } else {
            this.onPostArrangeAnInterview(values, this.mjson);
          }
        }
      });
    },
    fun(v) {
      if (this.positionType === "add") {
        v.activeDelivery = false;
        v.saveType = 1;
        return postDeliverRecords(v);
      }
      if (this.positionType === "edit") {
        v.id = this.deliverRecordId;
        return postAddDeliverRecord(v);
      }
    },
    async onPostDeliverRecords(v) {
      try {
        const { data } = await this.fun(this.positionValue);
        if (this.positionType === "add") {
          v.deliverRecordId = data.id;
          await this.onGetInfoById(v);
        }
        if (this.positionType === "edit") {
          v.deliverRecordId = data.id;
          const json = {
            talentPoolId: data.talentPoolId,
            companyUnitsName: data.companyUnitName,
            recruitInfoName: data.postName,
            companyUnitsId: data.companyUnitsId,
            recruitInfoId: data.recruitInfoId
          };
          await this.onPostArrangeAnInterview(v, json);
        }
      } catch (err) {
        this.confirmLoading = false;
        console.log("error log --> ", err);
      }
    },
    async onGetInfoById(v) {
      try {
        const { data } = await getInfoById(v.deliverRecordId);
        const json = {
          talentPoolId: data.talentPoolId,
          companyUnitsName: data.companyUnitName,
          recruitInfoName: data.postName,
          companyUnitsId: data.companyUnitsId,
          recruitInfoId: data.recruitInfoId
        };

        await this.onPostArrangeAnInterview(v, json);
      } catch (err) {
        this.confirmLoading = false;
        console.log("error log --> ", err);
      }
    }
  }
};
</script>

<style></style>
